// miniprogram/pages/address/address.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loading: true,

    addressList: [],

    url: '../edit_address/edit_address'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获取地址列表数据
    this.getAddressList();
  },


  //获取地址列表数据
  getAddressList() {
    //启动加载提示
    wx.showLoading({
      title: '加载中...',
      mask: true
    })
    //调用云函数【get_address】
    wx.cloud.callFunction({
      //云函数名称
      name: 'get_address',

      //成功执行
      success: result => {

        //关闭加载提示
        wx.hideLoading();

        console.log('调用云函数成功');
        console.log('result ==> ', result);

        result.result.data.map(v => {
          v.detailAddress = `${v.area.join('')}${v.detail}`;
        })

        this.setData({
          loading: false,
          addressList: result.result.data
        })



  
      },

      // 失败
      fail: err => {
        //关闭加载提示
        wx.hideLoading();
        
        console.log('err ==> ', err);
      }
    })
  },

  // 跳转编辑地址页面或者新增地址
  goPage(e) {
    let dataset = e.currentTarget.dataset;
    if (dataset._id) {
      dataset.url += '?_id=' + dataset._id;
    }
    wx.navigateTo({
      url: dataset.url
    })
  },

  //选择地址
  selectAddress(e) {
    console.log('e ==> ', e);
    //获取地址_id
    let _id = e.currentTarget.dataset._id;

    wx.navigateTo({
      url: '../pay/pay?aid=' + _id
    })

  }

})